package com.cd.music.service.impl;

import com.cd.music.dao.AdvertisingDao;
import com.cd.music.dto.ContentDto;
import com.cd.music.dto.ResultBean;
import com.cd.music.model.Advertising;
import com.cd.music.service.AdvertisingService;
import com.cd.music.utils.IdUtil;
import com.cd.music.utils.StringUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * @author yxl 余秀良
 * @version 1.0
 * @description: 用途
 * @date 2018年03月07日  上午9:24
 * @address: 四川成都
 */
@Transactional(readOnly = true, rollbackFor = Exception.class)
@Service(value = "advertisingService")
public class AdvertisingServiceImpl implements AdvertisingService {
    @Resource
    private AdvertisingDao advertisingDao;

    /**
     * 添加广告
     *
     * @param dto 参数
     * @return 受影响行数
     * @author yxl 余秀良
     * @version 1.0
     * @description: 添加广告
     * @date 2018年03月06日 下午10:20
     * @address: 四川成都
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultBean insert(ContentDto dto) {
        ResultBean.Builder builder = ResultBean.builder();
        if (dto != null) {
            List dataList = dto.getAdvertisingList();
            int total = 4;
            if (dataList != null && dataList.size() > 0 && dataList.size() <= total) {
                List<Advertising> advertisingList = new LinkedList<>();
                for (Object aDataList : dataList) {
                    Advertising advertising = new Advertising();
                    advertising.setId(IdUtil.getUUID());
                    advertising.setImage(aDataList.toString());
                    advertisingList.add(advertising);
                }
                boolean isOk = advertisingDao.insert(advertisingList) > 0;
                builder.success(isOk).message(isOk ? "添加成功" : "添加失败");
            } else {
                builder.success(false).message("添加失败,图片数量不能小于１大于４").code(400);
            }
        } else {
            builder.success(false).message("添加失败").code(400);
        }
        return builder.build();
    }

    /**
     * 删除广告
     *
     * @param id 广告id
     * @return 受影响行数
     * @author yxl 余秀良
     * @version 1.0
     * @description: 删除广告
     * @date 2018年03月06日 下午10:20
     * @address: 四川成都
     */
    @Override
    public ResultBean delete(String id) {
        ResultBean.Builder builder = ResultBean.builder();
        if (!StringUtil.isEmpty(id)) {
            boolean isOk = advertisingDao.delete(id) > 0;
            builder.success(isOk).message(isOk ? "删除成功" : "删除失败").code(isOk ? 200 : 500);
        } else {
            builder.code(400).message("参数错误").success(false);
        }
        return builder.build();
    }

    /**
     * 查询广告
     *
     * @param advertising 参数
     * @return 广告列表
     * @author yxl 余秀良
     * @version 1.0
     * @description: 查询广告
     * @date 2018年03月06日 下午10:20
     * @address: 四川成都
     */
    @Override
    public ResultBean select(ContentDto advertising) {
        List<Advertising> select = advertisingDao.select(advertising);
        return ResultBean.builder().data(select).message(select.size() > 0 ? "查询成功" : "没有数据").success(true).build();
    }

}
